import React, { Component } from 'react'
import { ListBox } from '../../../components'
import { fetchData } from '../../../components/AjaxConsumer'
import EditPage from './EditPage'
import InfoPage from './Info'
// import CalcPage from './Calc'
import OpenPage from './Open'
import CloseNewPage from './CloseNew'
import ListDetail from './ListDetailPage.js'
import UpdatePage from './Update'
import UpdateWorkareaPage from './UpdateNew'
import { Modal } from 'antd';

export default class ListRawStroagePage extends Component {
    constructor(props) {
        super(props);
        //行操作列表
        this.rowOperation = [
            {
                handleName: 'doubleClick',
                showName: '行双击事件'
            }
        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'close',
                showName: '冻结',
                iconClass: 'pushpin',
                permission: 'raw-freeze'
            },
            {
                handleName: 'open',
                showName: '解冻',
                iconClass: 'pushpin-o',
                permission: 'raw-unfreeze'
            },
            {
                handleName: 'modiContact',
                showName: '合同号修改',
                iconClass: 'edit',
                permission: 'raw-changeContact'
            }
            ,{
                handleName: 'info',
                showName: '查看',
                iconClass: 'info'
            },
            {
                handleName: 'export',
                showName: 'Excel',
                iconClass: 'file-excel'
            }
        ];
        //table列表展示列配置
        //storageKey 本地存储的key
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏（用于列配置显示隐藏）, foreverHidden: true 列永久隐藏
        this.tableColumn = {
            storageKey: 'odsRawStorageId',
            data: [
                {
                    showName: '合同号',
                    columnName: 'contractNo',
                    columnWidth: 120
                },
                {
                    showName: '型号',
                    columnName: 'model',
                    columnWidth: 120
                }, {
                    showName: '型号描述',
                    columnName: 'modelDesc',
                    columnWidth: 150
                }, {
                    showName: '型号类型',
                    columnName: 'modelType',
                    columnWidth: 90
                }, {
                    showName: '货主',
                    columnName: 'userDefined3',
                    showOption: [//页面显示配置
                        {
                            optionKey: '0',
                            optionShow: '本公司'
                        }, {
                            optionKey: '1',
                            optionShow: '外公司'
                        }
                    ],
                    columnWidth: 80
                }, {
                    showName: '库存个数',
                    columnName: 'amount',
                    columnWidth: 95
                }, {
                    showName: '单位',
                    columnName: 'unit',
                    columnWidth: 80
                }, {
                    showName: '库存数量',
                    columnName: 'iventAmount',
                    columnWidth: 95
                }, {
                    showName: '可用数量',
                    columnName: 'availAmount',
                    columnWidth: 95
                }, {
                    showName: '总冻结数量',
                    columnName: 'lockAmount',
                    columnWidth: 100
                }, 
                {
                    showName: '库位',
                    columnName: 'wlCode',
                    columnWidth: 100
                },
            ]
        };
        //搜索属性配置，如果字段不配置，默认是input搜索
        // 搜索类型：
        //      1、输入框 type='input'， 默认
        //      2、时间 type='date'
        //      3、数字 type='number'
        //      4、下拉框 type='select'
        //      5、下拉框 type='disabled'
        this.searchComponentData = [
            // {
            //     showName: '仓库',
            //     name: 'whCode',
            //     type: 'select',
            //     url: '/wmsrest/product/odsprostorage/getMdWareHouses',
            //     condition: { //查询条件

            //     }
            // },
            // {
            //     showName: '区域',
            //     name: 'wdCode',
            //     type: 'select',
            //     url: '/wmsrest/product/odsprostorage/getRegions',
            //     condition: { //查询条件

            //     }
            // },
            // {
            //     showName: '库区',
            //     name: 'waCode',
            //     type: 'select',
            //     url: '/wmsrest/product/odsprostorage/getMdAreas',
            //     condition: { //查询条件

            //     }
            // },
            {
                showName: '型号类型',
                name: 'modelType',
                type: 'select',
                optionList: [
                ]
            },
            {
                showName: '库存个数',
                name: 'amount',
                type: 'number',
                maxName: 'amountMax', //最大值
                minName: 'amountMin'  //最小值
            },
            {
                showName: '库存数量',
                name: 'iventAmount',
                type: 'number',
                maxName: 'iventAmountMax', //最大值
                minName: 'iventAmountMin'  //最小值
            },
            {
                showName: '可用数量',
                name: 'availAmount',
                type: 'number',
                maxName: 'availAmountMax', //最大值
                minName: 'availAmountMin'  //最小值
            }, {
                showName: '总冻结数量',
                name: 'lockAmount',
                type: 'number',
                maxName: 'lockAmountMax', //最大值
                minName: 'lockAmountMin'  //最小值
            }, {
                showName: '已分配数量',
                name: 'allocAmount',
                type: 'number',
                maxName: 'allocAmountMax', //最大值
                minName: 'allocAmountMin'  //最小值
            },
            {
                showName: '货主',
                name: 'userDefined3',
                type: 'select',
                optionList: [//下拉框的信息
                    //下拉框的信息
                    {
                        optionKey: '',
                        optionShow: ''
                    }, {
                        optionKey: '0',
                        optionShow: '本公司'
                    }, {
                        optionKey: '1',
                        optionShow: '外公司'
                    }
                ]
             }
        ];
    }
    rowHandles = (strHandles, rowObj) => {
        // if (strHandles === 'calc') {
        //     this.calcHandle(rowObj);
        // } else
         if (strHandles === 'close') {
            this.closeHandle(rowObj);
        } 
        else if (strHandles == 'doubleClick') {
            this.infoHandle(rowObj);
        } 
        else if (strHandles == 'open') {
            this.openHandle(rowObj);
        } 
        else if (strHandles == 'info') {
            this.infoHandle(rowObj);
        } 
        else if (strHandles == 'export') {
            this.exportXlsHandle(rowObj);
        }else if (strHandles == 'modiContact') {
             this.modiContactHandles(rowObj);
        }
    };

    modiContactHandles = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
            return;
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
            return;
        } else if (rowObj[0].data.modelType == 'BL' || rowObj[0].data.modelType == 'HL') {
            Modal.error({
                title: '错误提示',
                content: '黑白料类型不能修改合同号！',
            });
        } else {
            this.refs.updateWorkarea.editHandle(rowObj);
        }
    }

    exportXlsHandle = (conditonData) => {
        console.log('-------导出条件--------', conditonData);
        let param = '?'
        for (let key in conditonData) {
            if (conditonData[key] != '') {
                console.log(key, conditonData[key]);
                param = param + key + '=' + conditonData[key] + '&'
            }
        }
        console.log(param);
        window.location.href = '/material/insidewarehouse/RawStorage/listExp' + param;
    }

    // 调整分配数量
    calcHandle = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
            return;
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
            return;
        } else {
            // let pstData = {};
            // let tablesourse = [];
            // tablesourse[0] = rowObj[0].data;
            // this.refs.calcPage.editHandle(tablesourse, pstData);

            let pstData = {};
            pstData.wlCode = rowObj[0].data.wlCode;
            pstData.model = rowObj[0].data.model;
            pstData.modelType = rowObj[0].data.modelType;
            pstData.contractNo = rowObj[0].data.contractNo;
            pstData.allocAmount = rowObj[0].data.allocAmount;
            const options = {
                url: '/wmsrest/product/odsprostorage/adjust/allAMount/query',
                //url: '/wmsrest/product/odsprostoragesn/r/detail/',
                type: 'GET',
                condition: pstData,
            };
            fetchData(this, options, (resData) => {
                this.refs.calcPage.editHandle(resData.data, pstData);
            });
        }
    }

    closeHandle = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
            return;
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
            return;
        } else {
            let pstData = {};
            pstData.model = rowObj[0].data.model;
            pstData.modelType = rowObj[0].data.modelType;
            pstData.wlCode = rowObj[0].data.wlCode;
            pstData.contractNo = rowObj[0].data.contractNo;
            pstData.barcodeStatus = '0';
            const options = {
                url: '/material/insidewarehouse/RawStorageSn/r/detail',
                type: 'get',
                condition: pstData
            };
            fetchData(this, options, (resData) => {
                this.refs.closeNewPage.editHandle(resData, rowObj[0].data.RawStorageId,
                    pstData.wlCode,pstData.contractNo, pstData.model, pstData.modelType, pstData.barcodeStatus);
            });
        }
    };
    openHandle = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
            return;
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
            return;
        } else {
            let pstData = {};
            pstData.model = rowObj[0].data.model;
            pstData.modelType = rowObj[0].data.modelType;
            pstData.wlCode = rowObj[0].data.wlCode;
            pstData.contractNo = rowObj[0].data.contractNo;
            pstData.barcodeStatus = '1';
            console.log("rowObj[0].data.RawStorageId:****************" + rowObj[0].data.RawStorageId);
            const options = {
                url: '/material/insidewarehouse/RawStorageSn/r/detail',
                type: 'GET',
                condition: pstData,
            };
            fetchData(this, options, (resData) => {
                this.refs.openPage.editHandle(resData, rowObj[0].data.RawStorageId,
                    pstData.wlCode,pstData.contractNo, pstData.model, pstData.modelType, pstData.barcodeStatus);
            });
        }
    };

    infoHandle = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
            return;
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
            return;
        } else {
            let pstData = {};
            pstData.wlCode = rowObj[0].data.wlCode;
            pstData.contractNo = rowObj[0].data.contractNo;
            pstData.model = rowObj[0].data.model;
            pstData.modelType = rowObj[0].data.modelType;
            const options = {
                url: '/material/insidewarehouse/RawStorageSn/r/detail',
                type: 'GET',
                condition: pstData,
            };
            fetchData(this, options, (resData) => {
                this.refs.infoPage.editHandle(resData, pstData);
            });

            //this.refs.listDetail.editHandle(rowObj[0].data);
        }
    };


    componentWillMount() {
        const dicType = 'model_type';
        const options= {
            url: '/base/dic/get/' + dicType,
            type: 'get',
            condition:{}
        };
        //型号类型select
        fetchData(this, options, this.dicInitCallBackHandle);
    }

    //下拉框 初始化
    dicInitCallBackHandle = (resData)=>{
        let optionList = [];
        let tmp = {};
        tmp.optionKey = '';
        tmp.optionShow = '';
        optionList.push(tmp);
        resData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = e.dicCode;
            tmp.optionShow = e.dicName;
            optionList.push(tmp);
        });
        this.tableColumn.data[3].showOption = optionList;
        this.searchComponentData[0].optionList = optionList;
    }


    //刷新列表
    refreshHandle = () => {
        this.refs.listOdsRawstorage.refreshHandle();
    };
    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;
        return (
            <div>
                <ListBox
                    ref='listOdsRawstorage'
                    title="原材料库存查询"
                    isHideLeft={isHideLeft}
                    tableColumnData={this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url='/material/insidewarehouse/RawStorage/list'
                    rowHandles={this.rowHandles}
                    rowOperation={this.rowOperation}
                    headerOperation={this.headerOperation}
                    isRowSingle={true}
                   // subtractHeight='15'
                />
                <UpdatePage
                    ref="updatePage"
                    refreshHandle={this.refreshHandle}
                />
                
                <OpenPage
                    ref="openPage"
                    refreshHandle={this.refreshHandle}
                />

                <CloseNewPage
                    ref="closeNewPage"
                    refreshHandle={this.refreshHandle}
                />

                <InfoPage
                    ref="infoPage"
                    refreshHandle={this.refreshHandle}
                />

                <ListDetail
                    ref="listDetail"
                    refreshHandle = {this.refreshHandle}
                />

                <UpdateWorkareaPage
                    ref="updateWorkarea"
                    refreshHandle={this.refreshHandle}
                />
                {/* <CalcPage
                    ref="calcPage"
                    refreshHandle={this.refreshHandle}
                /> */}
            </div>
        )
    }
}